package com.example.nfc.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * Create by 游德禄 on 2020-2-17
 */
public class StyledWebView extends WebView {

    private static final String TAG    = "StyledWebView";
    public static String mDefaultEncoding = "UTF-8";
    private Context mContext;
    private String mCss;

    public StyledWebView(Context context) {
        this(context,null);
    }

    public StyledWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    public StyledWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs);
    }

    public StyledWebView(Context context, AttributeSet attrs, int defStyleAttr,
                         boolean privateBrowsing) {
        this(context, attrs);
    }

    public StyledWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        this.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        this.getSettings().setAllowUniversalAccessFromFileURLs(true);//允许跨域
        this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        this.getSettings().setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
        this.getSettings().setSupportZoom(false);//是否可以缩放，默认true
        this.getSettings().setUseWideViewPort(false);//设置此属性，可任意比例缩放。大视图模式
        this.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        this.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                result.confirm();
                return true;
            }
            @Override
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                AlertDialog.Builder b = new AlertDialog.Builder(mContext);
                b.setTitle("");
                b.setMessage(message);
                b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                    }
                });
                b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.cancel();
                    }
                });
                b.create().show();
                return true;
            }
        });

    }

    private String injectCss(String page, String css) {
        int headEnd = page.indexOf("</head>");
        String res = "";
        if (headEnd > 0) {
            res = page.substring(0, headEnd) + css + page.substring(headEnd, page.length());
        } else {
            res = "<head>" + css + "</head>" + page;
        }
        return res;
    }

    private String buildCss() {
        StringBuilder contents = new StringBuilder();

        InputStreamReader reader;
        try {
            reader = new InputStreamReader(mContext.getAssets().open(mCss), mDefaultEncoding);
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                contents.append(line);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "<style type=\"text/css\">" + contents.toString().trim().replace("\n", "") + "</style>";
    }
    public void setDefaultEncoding(String encoding) {
        mDefaultEncoding = encoding;
    }
}
